{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Architecture:        x86_64\n",
      "CPU op-mode(s):      32-bit, 64-bit\n",
      "Byte Order:          Little Endian\n",
      "CPU(s):              8\n",
      "On-line CPU(s) list: 0-7\n",
      "Thread(s) per core:  2\n",
      "Core(s) per socket:  4\n",
      "Socket(s):           1\n",
      "NUMA node(s):        1\n",
      "Vendor ID:           GenuineIntel\n",
      "CPU family:          6\n",
      "Model:               60\n",
      "Model name:          Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz\n",
      "Stepping:            3\n",
      "CPU MHz:             3156.798\n",
      "CPU max MHz:         3400.0000\n",
      "CPU min MHz:         800.0000\n",
      "BogoMIPS:            4788.62\n",
      "Virtualisation:      VT-x\n",
      "L1d cache:           32K\n",
      "L1i cache:           32K\n",
      "L2 cache:            256K\n",
      "L3 cache:            6144K\n",
      "NUMA node0 CPU(s):   0-7\n",
      "Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d\n"
     ]
    }
   ],
   "source": [
    "!lscpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sun Apr  5 16:19:17 2020       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 440.64.00    Driver Version: 440.64.00    CUDA Version: 10.2     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  Quadro K1100M       On   | 00000000:01:00.0 Off |                  N/A |\n",
      "| N/A   47C    P5    N/A /  N/A |    408MiB /  1999MiB |      0%      Default |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                       GPU Memory |\n",
      "|  GPU       PID   Type   Process name                             Usage      |\n",
      "|=============================================================================|\n",
      "|    0      1149      G   /usr/lib/xorg/Xorg                            39MiB |\n",
      "|    0      1411      G   /usr/bin/gnome-shell                          51MiB |\n",
      "|    0      2007      G   /usr/lib/xorg/Xorg                           132MiB |\n",
      "|    0      2182      G   /usr/bin/gnome-shell                          90MiB |\n",
      "|    0      2670      G   ...uest-channel-token=14154503548353210850    30MiB |\n",
      "|    0     11443      G   /usr/lib/firefox/firefox                      21MiB |\n",
      "|    0     11471      G   /usr/lib/firefox/firefox                      22MiB |\n",
      "|    0     11494      G   /usr/lib/firefox/firefox                       8MiB |\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting features...\n",
      "Progress 1491/1491 - ETA: 0:00:00.094553\n",
      "2073967 features extracted\n"
     ]
    }
   ],
   "source": [
    "import cbir\n",
    "\n",
    "dataset = cbir.Dataset()\n",
    "\n",
    "orb = cbir.descriptors.Orb()\n",
    "voc = cbir.encoders.VocabularyTree(n_branches=4, depth=4, descriptor=orb)\n",
    "\n",
    "features = voc.extract_features(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1min 43s, sys: 3.17 s, total: 1min 46sde 336 at level 3\t\t\t\n",
      "Wall time: 56.6 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "voc.fit(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = cbir.Database(dataset, encoder=voc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Generating index...\n",
      "CPU times: user 9min 2s, sys: 3.85 s, total: 9min 6s\n",
      "Wall time: 8min 46s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "db.index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "query = random.choice(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 73.1 ms, sys: 7 µs, total: 73.1 ms\n",
      "Wall time: 72.5 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "scores = db.retrieve(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
